const { contextBridge, ipcRenderer } = require("electron");

//向渲染进程暴露API
contextBridge.exposeInMainWorld("electronAPI", {
  // 发送消息到主线程
  sendMessage: (channel, data) => {
    ipcRenderer.send(channel, data);
  },
  // 接收主线程消息
  onMessage: (channel, callback) => {
    //过滤频道，确保安全
    const validChannels = ["message-from-main", "update-ui"];
    if (validChannels.includes(channel)) {
      const listener = (event, ...args) => callback(...args);
      ipcRenderer.on(channel, listener);
      return () => ipcRenderer.removeListener(channel, listener);
    }
  },
});

window.addEventListener("DOMContentLoaded", () => {
  const replaceText = (selector, text) => {
    const element = document.getElementById(selector);
    if (element) element.innerText = text;
  };

  for (const dependency of ["chrome", "node", "electron"]) {
    replaceText(`${dependency}-version`, process.versions[dependency]);
  }
});
